package com.lkd.job;

import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class TaskInitJob {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每日工单量列表初始化
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{
        //todo:查询用户列表
        //获取用户详情列表
        List<UserEntity> list = userService.list();

        int plusday = 1;
        if ("today".equals(param)) {
            plusday = 0;
        }
        String date = LocalDate.now().plusDays(plusday).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        //遍历获取每个用户详情
        for (UserEntity userEntity : list) {
            //以固定字符串（前缀）+日期+区域+工单类别（运营/运维）为大key
            String key = "taskInit."+ date + userEntity.getRegionId()+ userEntity.getRoleCode();
            //以人员id做小key，过期时间为2天
            redisTemplate.expire(key,Duration.ofDays(2));
        }
        //todo:构建数据（zset）

        return ReturnT.SUCCESS;
}

}
